/**
 *
 * @author AD_DA
 */
package BL;

import Bean.BMenu;
import Bean.BSubMenu;
import Bean.BSupMenu;
import Util.DBConnection;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class BLMenu {
    private   List<BSubMenu> BSubMenus =new ArrayList<BSubMenu>();  
    private  void initSubMenu() throws SQLException, FileNotFoundException, IOException{        
        Connection conn = DBConnection.getConnection();
        if (conn != null) {
            Statement stm = (Statement) conn.createStatement();
            String sql = "SELECT * FROM  loaihinhdaotao";
            ResultSet rs = stm.executeQuery(sql);
            
            while (rs.next()) {
                BSubMenu BM = new BSubMenu();
                BM.setIDSubMenu(rs.getInt("MaLHDT"));
                BM.setIDAlias(rs.getString("MaHeDT"));
                BM.setNameSubMenu(rs.getString("TenLHDT"));
                BM.setState(false);                
                BSubMenus.add(BM);                
            }  
            sql = "SELECT * FROM  NhomNganh";
            rs = stm.executeQuery(sql);            
            while (rs.next()) {
                BSubMenu BM = new BSubMenu();
                BM.setIDSubMenu(rs.getInt("MaNN"));
                BM.setIDAlias("0999999998");
                BM.setNameSubMenu(rs.getString("TenNN"));
                BM.setState(false);                
                BSubMenus.add(BM);                
            }  
            sql = "SELECT * FROM  khuvuc";
            rs = stm.executeQuery(sql);            
            while (rs.next()) {
                BSubMenu BM = new BSubMenu();
                BM.setIDSubMenu(rs.getInt("MaKV"));
                BM.setIDAlias("999999999");
                BM.setNameSubMenu(rs.getString("TenKV"));
                BM.setState(false);                
                BSubMenus.add(BM);                
            }  
        }
        
    }
    private  List<BSubMenu> getSubMenu(int IDSupMenu, int Alias)
    {
        List<BSubMenu> SubMenu =new ArrayList<BSubMenu>();
        int k=1;
        for(int i=0; i<BSubMenus.size();i++)
        {
            if(Integer.parseInt(BSubMenus.get(i).getIDAlias())==IDSupMenu)
                SubMenu.add(new BSubMenu(""+Alias+(k++),BSubMenus.get(i).getIDSubMenu(),BSubMenus.get(i).getNameSubMenu(),BSubMenus.get(i).getState()));
        }
        return SubMenu;
    }
    public  List<BMenu> getMenu() throws SQLException, FileNotFoundException, IOException{
        
        List<BMenu> Menus =new ArrayList<BMenu>();
        initSubMenu();
        Connection conn = DBConnection.getConnection();
        if (conn != null) {
            Statement stm = (Statement) conn.createStatement();
            String sql = "SELECT * FROM  HeDaoTao";
            ResultSet rs = stm.executeQuery(sql);
            BMenu BM = new BMenu();
            int k=0;
            while (rs.next()) {  
                BM = new BMenu();
                BM.setIDSupMenu(rs.getInt("MaHeDT"));
                BM.setNameSupMenu(rs.getString("TenHeDT"));                
                BM.setState(false);
                BM.setSubMenus(getSubMenu(BM.getIDSupMenu(),(k++) ));
                Menus.add(BM);                
            }
            conn.close();
            BM = new BMenu();
            BM.setIDSupMenu(999999998);
            BM.setNameSupMenu("NHÓM NGÀNH");                
            BM.setState(false);  
            BM.setSubMenus(getSubMenu(BM.getIDSupMenu(),(k++) ));
            Menus.add(BM);
            
            BM = new BMenu();
            BM.setIDSupMenu(999999999);
            BM.setNameSupMenu("KHU VỰC");                
            BM.setState(false); 
            BM.setSubMenus(getSubMenu(BM.getIDSupMenu(),(k++) ));
            Menus.add(BM);
            
                         
        }
          
        return Menus;
    }
    public  List<BSupMenu> getSupMenuWithActive(String IDSup) throws SQLException, FileNotFoundException, IOException{
        List<BSupMenu> BSupMenus =new ArrayList<BSupMenu>();  
        Connection conn = DBConnection.getConnection();
        if (conn != null) {
            Statement stm = (Statement) conn.createStatement();
            String sql = "SELECT * FROM  HeDaoTao";
            ResultSet rs = stm.executeQuery(sql);
            BSupMenu BSM = new BSupMenu();
            while (rs.next()) {                
                BSM.setIDSupMenu(rs.getInt("MaHeDT"));                
                BSM.setNameSupMenu(rs.getString("TenHeDT"));
                if(Integer.parseInt(IDSup)==BSM.getIDSupMenu())
                    BSM.setState(true);
                else BSM.setState(false);                
                BSupMenus.add(BSM);                
            }            
            BSM.setIDSupMenu(999999998);                
            BSM.setNameSupMenu("NHÓM NGÀNH");
            BSM.setState(false);                
            BSupMenus.add(BSM);
            BSM.setIDSupMenu(999999999);                
            BSM.setNameSupMenu("KHU VỰC");
            BSM.setState(false);                
            BSupMenus.add(BSM);             
        }
        return BSupMenus;
    }
    
    public  void main(String[] arg) throws FileNotFoundException, IOException
    {
        try{
            List<BMenu> Menus=null;
            Menus=getMenu();
            for(int i=0;i<Menus.size();i++)
            {
                System.out.println(Menus.get(i).getIDSupMenu());
            }
        }catch(SQLException ex){}
    }
}
